<?php

class RSRecordList
{
	const TYPE_SKILL = 1;
	const TYPE_ACTIVITY = 2;

	private static $records = array(
		'overall' => array(
			'name' => 'Overall', 'id' => 0,
			'key' => 'overall', 'type' => self::TYPE_SKILL),
		'attack' => array(
			'name' => 'Attack', 'id' => 1,
			'key' => 'attack', 'type' => self::TYPE_SKILL),
		'defence' => array(
			'name' => 'Defence', 'id' => 2,
			'key' => 'defence', 'type' => self::TYPE_SKILL),
		'strength' => array(
			'name' => 'Strength', 'id' => 3,
			'key' => 'strength', 'type' => self::TYPE_SKILL),
		'constitution' => array(
			'name' => 'Constitution', 'id' => 4,
			'key' => 'constitution', 'type' => self::TYPE_SKILL),
		'ranged' => array(
			'name' => 'Ranged', 'id' => 5,
			'key' => 'ranged', 'type' => self::TYPE_SKILL),
		'prayer' => array(
			'name' => 'Prayer', 'id' => 6,
			'key' => 'prayer', 'type' => self::TYPE_SKILL),
		'magic' => array(
			'name' => 'Magic', 'id' => 7,
			'key' => 'magic', 'type' => self::TYPE_SKILL),
		'cooking' => array(
			'name' => 'Cooking', 'id' => 8,
			'key' => 'cooking', 'type' => self::TYPE_SKILL),
		'woodcutting' => array(
			'name' => 'Woodcutting', 'id' => 9,
			'key' => 'woodcutting', 'type' => self::TYPE_SKILL),
		'fletching' => array(
			'name' => 'Fletching', 'id' => 10,
			'key' => 'fletching', 'type' => self::TYPE_SKILL),
		'fishing' => array(
			'name' => 'Fishing', 'id' => 11,
			'key' => 'fishing', 'type' => self::TYPE_SKILL),
		'firemaking' => array(
			'name' => 'Firemaking', 'id' => 12,
			'key' => 'firemaking', 'type' => self::TYPE_SKILL),
		'crafting' => array(
			'name' => 'Crafting', 'id' => 13,
			'key' => 'crafting', 'type' => self::TYPE_SKILL),
		'smithing' => array(
			'name' => 'Smithing', 'id' => 14,
			'key' => 'smithing', 'type' => self::TYPE_SKILL),
		'mining' => array(
			'name' => 'Mining', 'id' => 15,
			'key' => 'mining', 'type' => self::TYPE_SKILL),
		'herblore' => array(
			'name' => 'Herblore', 'id' => 16,
			'key' => 'herblore', 'type' => self::TYPE_SKILL),
		'agility' => array(
			'name' => 'Agility', 'id' => 17,
			'key' => 'agility', 'type' => self::TYPE_SKILL),
		'thieving' => array(
			'name' => 'Thieving', 'id' => 18,
			'key' => 'thieving', 'type' => self::TYPE_SKILL),
		'slayer' => array(
			'name' => 'Slayer', 'id' => 19,
			'key' => 'slayer', 'type' => self::TYPE_SKILL),
		'farming' => array(
			'name' => 'Farming', 'id' => 20,
			'key' => 'farming', 'type' => self::TYPE_SKILL),
		'runecrafting' => array(
			'name' => 'Runecrafting', 'id' => 21,
			'key' => 'runecrafting', 'type' => self::TYPE_SKILL),
		'hunter' => array(
			'name' => 'Hunter', 'id' => 22,
			'key' => 'hunter', 'type' => self::TYPE_SKILL),
		'construction' => array(
			'name' => 'Construction', 'id' => 23,
			'key' => 'construction', 'type' => self::TYPE_SKILL),
		'summoning' => array(
			'name' => 'Summoning', 'id' => 24,
			'key' => 'summoning', 'type' => self::TYPE_SKILL),

		'dueltournament' => array(
			'name' => 'Duel Tournament', 'id' => 0,
			'key' => 'dueltournament', 'type' => self::TYPE_ACTIVITY),
		'bountyhunters' => array(
			'name' => 'Bounty Hunters', 'id' => 1,
			'key' => 'bountyhunters', 'type' => self::TYPE_ACTIVITY),
		'bountyhunterrogues' => array(
			'name' => 'Bounty Hunter rogues', 'id' => 2,
			'key' => 'bountyhunterrogues', 'type' => self::TYPE_ACTIVITY),
		'fistofguthix' => array(
			'name' => 'Fist of Guthix', 'id' => 3,
			'key' => 'fistofguthix', 'type' => self::TYPE_ACTIVITY),
		'mobilisingarmies' => array(
			'name' => 'Mobilising Armies', 'id' => 4,
			'key' => 'mobilisingarmies', 'type' => self::TYPE_ACTIVITY),
		'baattackers' => array(
			'name' => 'B.A Attackers', 'id' => 5,
			'key' => 'baattackers', 'type' => self::TYPE_ACTIVITY),
		'badefenders' => array(
			'name' => 'B.A Defenders', 'id' => 6,
			'key' => 'badefenders', 'type' => self::TYPE_ACTIVITY),
		'bacollectors' => array(
			'name' => 'B.A Collectors', 'id' => 7,
			'key' => 'bacollectors', 'type' => self::TYPE_ACTIVITY),
		'bahealers' => array(
			'name' => 'B.A Healers', 'id' => 8,
			'key' => 'bahealers', 'type' => self::TYPE_ACTIVITY),
	);

	public static function getRecords ()
	{
		return self::$records;
	}

	public static function getRecordCount ()
	{
		return count(self::$records);
	}

	public static function getSkills ()
	{
		return self::getRecordsByType(self::TYPE_SKILL);
	}

	public static function getActivities ()
	{
		return self::getRecordsByType(self::TYPE_ACTIVITY);
	}

	private static function getRecordsByType ($type)
	{
		$records = array();

		foreach (self::$records as $key => $record)
		{
			if ($record['type'] === $type)
			{
				$records[$key] = $record;
			}
		}

		return $records;
	}

	public static function getRecord ($record)
	{
		// Quick check if appropriate record exists
		if (isset(self::$records[$record]))
		{
			return self::$records[$record];
		}

		$numeric = is_numeric($record);

		// If it's not numeric, just check if someone used wrong capitalization
		if (!$numeric)
		{
			if (isset(self::$records[strolower($record)]))
			{
				return self::$records[strolower($record)];
			}
		}

		// Go through all the records and see if we used name or ID
		foreach  (self::$records as $item)
		{
			$found = $numeric
				? $item['id'] === (int) $record
				: strcasecmp($record, $item['name']) === 0;

			// Ternary operator just looks funny inside an IF
			if ($found)
			{
				return $item;
			}
		}

		throw new InvalidRecordException("'$record' is not a valid character record");
	}

	public static function getSkill ($skill)
	{
		$skill = self::getRecord($skill);

		if ($skill['type'] !== self::TYPE_SKILL)
		{
			throw new InvalidSkillException("'$skill' is not a skill record");
		}

		return $skill;
	}

	public static function getActivity ($activity)
	{
		$activity = self::getRecord($activity);

		if ($activity['type'] !== self::TYPE_ACTIVITY)
		{
			throw new InvalidActivityException("'$activity' is not an activity record");
		}

		return $activity;
	}
}